package cn.edu.hyit.zhao.task;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.edu.hyit.zhao.dao.IControlTypeDAO;
import cn.edu.hyit.zhao.dao.IRecordDAO;
import cn.edu.hyit.zhao.dao.IShopDAO;
import cn.edu.hyit.zhao.entity.ControlType;
import cn.edu.hyit.zhao.entity.Record;
import cn.edu.hyit.zhao.entity.Shop;

@Component
public class ShopRecordProduce {
    @Autowired
    private IShopDAO shopDao;
    @Autowired
    private IControlTypeDAO controlTypeDao;
    @Autowired
    private IRecordDAO recordDao;

    @Scheduled(cron = "0/60 * *  * * ? ")
    public void doJob() {
	List<Shop> shops = shopDao.selectAllStartedShop("1");
	Random random = new Random(System.currentTimeMillis());
	ControlType controlType = null;
	Record record = null;
	for (Shop shop : shops) {
	    controlType = controlTypeDao.selectByPrimaryKey(shop.getControlId());
	    if (null == controlType) {
		continue;
	    }
	    Date date = new Date(System.currentTimeMillis());
	    if (date.compareTo(shop.getLimitTime()) <= 0) {
		record = new Record();
		record.setShopId(shop.getShopId());
		record.setCollectTime(new Date());
		Integer humidity = random.nextInt((controlType.getMaxHumidity() - controlType.getMinHumidity() + 1)
			+ controlType.getMinHumidity());
		Integer temperature = random
			.nextInt((controlType.getMaxTemperature() - controlType.getMinTemperature() + 1)
				+ controlType.getMinTemperature());
		record.setHumidity(humidity.toString());
		record.setTemperature(temperature.toString());
		record.setWaring("正常");
		recordDao.insertSelective(record);
		System.out.println("产生了一条数据" + record);
	    }
	}
    }
}
